package com.wedo.book.controller;

import com.wedo.book.pojo.Book;
import com.wedo.book.pojo.BookHot;
import com.wedo.book.result.BookHotResult;
import com.wedo.book.service.BookService;
import com.wedo.common.pojo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("books")
public class BookController {

    @Autowired
    private BookService bookService;

    @GetMapping("page")
    public ResponseEntity<PageResult<Book>> queryBookByPage(
            @RequestParam(value = "title",required = false) String title,
            @RequestParam(value = "cid",required = false) Long cid,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "6") Integer rows
    ) {
        PageResult<Book> result = this.bookService.queryBookByPage(title,cid,page,rows);
        if (result == null || CollectionUtils.isEmpty(result.getItems())) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 根据分类id查询
     * @param cid
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("pageByCid")
    public ResponseEntity<PageResult<Book>> queryBookPageByCid(
            @RequestParam(value = "cid",required = true) Long cid,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "6") Integer rows
    ) {
        PageResult<Book> result = this.bookService.queryBookPageByCid(cid,page,rows);
        if (result == null || CollectionUtils.isEmpty(result.getItems())) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 根据分类id查询
     * @param cid
     * @param rows
     * @return
     */
    @GetMapping("pageByCidAndHot")
    public ResponseEntity<List<BookHotResult>> queryBooksByCidAndHot(
            @RequestParam(value = "cid") Long cid,
            @RequestParam(value = "rows",defaultValue = "4") Integer rows
    ) {
        List<BookHotResult> books = this.bookService.queryBooksByCidAndHot(cid,rows);
        if (CollectionUtils.isEmpty(books)) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }
        return ResponseEntity.ok(books);
    }

    /**
     * 根据作者id查询
     * @param aid
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("pageByAid")
    public ResponseEntity<PageResult<Book>> queryBookPageByAid(
            @RequestParam(value = "aid",required = true) Long aid,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "6") Integer rows
    ) {
        PageResult<Book> result = this.bookService.queryBookPageByAid(aid,page,rows);
        if (result == null || CollectionUtils.isEmpty(result.getItems())) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 根据图书id查找
     * @param bid
     * @return
     */
    @GetMapping("bid/{bid}")
    public ResponseEntity<Book> queryBookByBid(@PathVariable("bid")Long bid) {
        Book result = this.bookService.queryBookByBid(bid);
        if (result == null) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }
        return ResponseEntity.ok(result);
    }

    @PutMapping
    public ResponseEntity<Book> updateBook(@RequestBody Book book) {
        if (book.getId() == null) {
            return ResponseEntity.badRequest().build();
        }
        this.bookService.updateBookById(book);
        return ResponseEntity.ok(book);
    }

    @PostMapping
    public ResponseEntity<Book> addBook(@RequestBody Book book) {
        this.bookService.saveBook(book);
        return ResponseEntity.ok(book);
    }

    @DeleteMapping
    public ResponseEntity<Book> deleteBook(@RequestBody Book book) {
        if (book.getId() == null) {
            return ResponseEntity.badRequest().build();
        }
        this.bookService.deleteBook(book.getId());
        return ResponseEntity.ok(book);
    }

}
